<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/
defined('_JEXEC') or die('Restricted access');
require_once('import.php');
require_once('defines.php');

class JV {     
     protected $document  = null;
     protected $parameter = null;
     protected $html      = null;
     protected $_activeTheme;
     protected $_defaultTheme;
     protected $_themeAssignList;
     
     /**
     * Browser warning.
     * 
     * @var bool $warning
     * @access public
     */
    public $warning = false;
    
    function JV(){
		$args = func_get_args();
		call_user_func_array(array(&$this, '__construct'), $args);
	}
    
    /**
     * constructor method
     * 
     * @param object $document JDocumentHTML object.
     * @access public  
     * @return void
     */
   	public function __construct($document = false) {
        $this->document = $document;
        JHTML::_('behavior.mootools');
    }
    
    /**
     * Get Instance JVFrameWork
     * 
     * @param  object $document JDocumentHTML object.
     * @access public     
     * @return object JV
     */
    public static function &getInstance($document = null) {        
        static $instance = null;        
        if (!isset($instance)) {
            $instance = new JV($document);
        }
        return $instance;
    }
    
    
    /**
     * Initialise
     * 
     * @access public
     * @return void
     */
    public function initialise(){
        $params = JVFactory::getParameter();        
        if(!JFolder::exists(JVPATH_THEME.$this->getActiveTheme()->theme)){
           JError::raiseError('Theme not found','Please check theme folder name, reinstall theme or use other theme !');
        }
        
        if($params->enableSeokey()){
            if($params->getSeoKey() != array()){
                $content = JVSEO::JVKeySeo($this->document->getBuffer('component'), $params->getSeoKey());
                $this->document->setBuffer($content,'component');
            }
        }
        
        if(JVFactory::getUserAgent() != 'standard'){
            $this->addMetaTags('viewport','width=device-width; initial-scale=1.0; maximum-scale=5.0; user-scalable=1;');
            $this->addMetaTags('apple-touch-fullscreen','YES');
            $this->addMetaTags('HandheldFriendly','true');
        }
        $this->html =  JVLayout::getInstance($params)->getLayout();
    }
    
    /**
     * Render doccument
     * 
     * @access public
     * @return string $html
     */
    public function render(){ 
        $html = Minify_HTML::minify($this->html, array('xhtml' => true)); // Minify html. 
          
        if ($html) {
            if ($this->isWarning()) {
                $html .= JVHooks::warning();
            }              
            echo $html;
        }
       
    }
    
    /**
     * Get active theme 
     * 
     * @access public
     * @return void
     */
    public function getActiveTheme() {   
        if($this->_activeTheme == null){
            $theme = $this->getDefautTheme();
            $themeAssignList = $this->getThemeAssignList();            
            $db =& JFactory::getDBO();
             
            #Get active menu
            $menu =& JSite::getMenu();
            $defaultMenu = $menu->getActive();
            
            if($defaultMenu != ''){
                $itemid = $defaultMenu->id;
            
                if(count($themeAssignList)){
                    foreach($themeAssignList as $themeAssign){                
                        if($themeAssign['menuid'] == $itemid ){ 
                            $db->setQuery('SELECT id, theme FROM #__jv_themes WHERE home<>2 AND id='.$themeAssign['themeid']);                 
                            $resulf = $db->loadObject();                            
                            if(!is_null($resulf)){
                                $resulf->theme = current(explode('|', $resulf->theme));
                                $theme = $resulf;
                            }
                        }
                    }
                }
                
            }
            if(is_object($theme)){
                $this->_activeTheme  = $theme;   
            }else{
                JError::raiseError('Theme not found','Please set default theme or assign a theme to this page !');
            }
        }
             
        return $this->_activeTheme;
    }
    
    /**
     * Theme assign list. 
     * 
     * @access public
     * @return void
     */
    public function getThemeAssignList() {
        if($this->_themeAssignList == null){
            $data =& JFactory::getDBO();
            $data->setQuery('SELECT * FROM #__jv_theme_assign');
            $menuAssign = $data->loadAssocList();
            $this->_themeAssignList = $menuAssign;
        }
        
        return $this->_themeAssignList;
    }
    
    /**
     * Get default theme 
     * 
     * @access public
     * @return void
     */
    public function getDefautTheme() {
        if($this->_defaultTheme == null){
            $data =& JFactory::getDBO();
            $data->setQuery('SELECT id, theme FROM #__jv_themes WHERE home=1');
            $this->_defaultTheme = $data->loadObject();
            if(is_object($this->_defaultTheme)){
                $this->_defaultTheme->theme = current(explode('|', $this->_defaultTheme->theme));
            }
        }
        
        return $this->_defaultTheme;
    }
    
    /**
     * JDocumentHTML Object method get
     *
     * @access public  
     * @return 
     */
    public function get($var) {
        $method = 'get' . ucfirst($var);
        return $this->document->$method();
    }

    /**
     * JDocumentHTML Object method set 
     *
     * @access public   
     * @return void
     */
    public function set($var, $value) {
        $method = 'set' . ucfirst($var);
        $this->document->$method($value);
    }
    
    /**
     * Get JDocumentHTML Object
     *
     * @access public  
     * @return object document
     */
    public function getDocument() {
        return $this->document;
    }
    
    /**
     * Set Enable Browser Warning. 
     * 
     * @access public
     * @return void
     */
    function enableWarning() {
        $this->warning = true;
    }

    /**
     * Check Enable Browser Warning
     * 
     * @access public  
     * @return bool True, false.     
     */
    function isWarning() {
        return $this->warning || false;
    }
    
    /**
     * Add custom metatag
     * 
     * @access public.      
     * @return void.
     */
    function addMetaTags($name, $content){
        $this->document->_metaTags['standard'] += array($name => $content);
        return;
    }

    
    
}

